﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic;
using Meqs.Logic.Interface;
using System.Collections.Generic;

public partial class Moral_Teacher_ScoreInput_WeekOverview : ContainerBasePage
{
    protected static int EvalID
    {
        get
        {
            int _evalId;
            if (int.TryParse(HttpContext.Current.Request["id"], out _evalId))
                return _evalId;
            return -1;
        }
    }

    protected static int Attribute
    {
        get
        {
            int _attribute;
            if (int.TryParse(HttpContext.Current.Request["attribute"], out _attribute))
                return _attribute;
            return -1;
        }
    }

    protected static int EvalType
    {
        get
        {
            int _evalType;
            if (int.TryParse(HttpContext.Current.Request["evaltype"], out _evalType))
                return _evalType;
            return -1;
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitDisplay();
        }
    }
    /// <summary>
    /// 初始化界面标签元素的显示行为
    /// </summary>
    protected void InitDisplay()
    {
        lbTitle.Text = "";
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("IsEffect", "1");
        if (EvalType == 1)
            ht.Add("HeaderID", Common.GetUserInfo().UID);
        IList<ClsInfo> clsList = Container<IClsInfoLogic>.Instance.FindAll(ht);


        IList<int> idList = new List<int>();
        foreach (ClsInfo info in clsList)
        {
            idList.Add(info.RID);
        }

        IList<MoralityState> stateList =
        Container<IMoralityHistoryLogic>.Instance.GetState(SchoolCommon.Sch_SchoolInfo.SchoolYear,
                                                       SchoolCommon.Sch_SchoolInfo.SchoolTerm, "Student_Month_Action",
                                                       SchoolMark, idList);
        CreateMoralityTable(clsList, stateList,
            SchoolCommon.Sch_SchoolInfo.SchoolYear,
            SchoolCommon.Sch_SchoolInfo.SchoolTerm);
        lbTitle.Text = "德育表现评价录入";
    }

    /// <summary>
    /// 构造 学生日常行为规范 结果表格
    /// </summary>
    private void CreateMoralityTable(IList<ClsInfo> classInfo, IList<MoralityState> stateInfo, int year, int term)
    {
        int schoolYear = SchoolCommon.Sch_SchoolInfo.SchoolYear;
        int width = 90;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hrow.Cells.Add(hcell);

        IList<DateTime> dateList = new List<DateTime>();
        ListSort<MoralityState> sort = new ListSort<MoralityState>(stateInfo, "StartTime", false);
        stateInfo = sort.Sort();

        Hashtable ht = new Hashtable();
        ht.Add("SchoolYear", schoolYear);
        ht.Add("Term", term);
        IList<TermInfo> infoList = Container<ITermInfoLogic>.Instance.FindAll(ht);
        if (infoList == null || infoList.Count < 1)
            return;
        DateTime termStart = (DateTime)infoList[0].StartDate;
        int weeks = 0, month = 0, selYear = 0;

        SchoolCommon.GetWeekOfTerm(SchoolCommon.Sch_SchoolInfo.SchoolYear, SchoolCommon.Sch_SchoolInfo.SchoolTerm, SchoolMark, DateTime.Now, out weeks);
        int currWeek = 0;
        DateTime timeStart = new DateTime();//开始时间
        DateTime tiemEnd = new DateTime();
        for (int state = 0; state <= weeks; state++)
        {
            SchoolCommon.GetWeekInTerm(schoolYear, term, SchoolMark, (state + 1), out timeStart, out tiemEnd);
            hcell = new TableHeaderCell();
            hcell.Text = String.Format("{0}周", state + 1);
            hcell.Style[HtmlTextWriterStyle.Width] = "75px";
            width += 75;
            hrow.Cells.Add(hcell);
            dateList.Add(timeStart);
            timeStart = timeStart.AddDays(7);
            //AddMonths(state));

            if (state == currWeek - 1)
                break;
        }
        tbResult.Rows.Add(hrow);
        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);

        // 填充表内容
        TableRow row = null;
        TableCell cell = null;
        foreach (ClsInfo info in classInfo)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = info.ClsName;
            row.Cells.Add(cell);


            int i = 0;
            foreach (DateTime dt in dateList)
            {
                cell = new TableCell();
                bool sign = false;
                foreach (MoralityState state in stateInfo)
                {
                    if (state.ClassID == info.RID && state.StartTime == dt)
                    {
                        // 构造超链html标签
                        HyperLink control = new HyperLink();
                        control.Text = "已录入";
                        control.Attributes.Add("onclick", "detailView();");
                        control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Day={5}&Type=Edit&weekIndex=" + (i+1),
                                                year, term,
                                                state.ClassID, state.StartTime.Year, state.StartTime.Month, state.StartTime.Day);
                        cell.Controls.Add(control);
                        sign = true;
                        break;
                    }
                }
                if (!sign)
                {
                    // 构造超链html标签
                    HyperLink control = new HyperLink();
                    control.Text = "未录入";
                    control.Attributes.Add("onclick", "detailView();");
                    control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Day={5}&Type=Edit&weekIndex=" + (i + 1),
                                            year, term,
                                            info.RID, dt.Year, dt.Month, dt.Day);
                    cell.Controls.Add(control);
                }
                cell.Attributes.Add("onmouseout", "mout(" + i + ");");
                cell.Attributes.Add("onmouseover", "mover(" + i + ");");
                row.Cells.Add(cell);
                i++;
            }


            tbResult.Rows.Add(row);
        }
        //while (!((termStart.Month > DateTime.Now.Month || termStart.Year > DateTime.Now.Year) && termStart > DateTime.Now))
        //{

        //    hcell = new TableHeaderCell();
        //    hcell.Text = String.Format("{0}月", weeks);
            
        //    hcell.Style[HtmlTextWriterStyle.Width] = "75px";
        //    width += 75;
        //    hrow.Cells.Add(hcell);
        //    dateList.Add(termStart);
        //    termStart = termStart.AddMonths(1);

        //}
        //tbResult.Rows.Add(hrow);

        //tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);

        //// 填充表内容
        //TableRow row = null;
        //TableCell cell = null;
        //foreach (ClsInfo info in classInfo)
        //{
        //    row = new TableRow();
        //    cell = new TableCell();
        //    cell.Text = info.ClsName;
        //    row.Cells.Add(cell);

        //    foreach (DateTime dt in dateList)
        //    {
        //        cell = new TableCell();
        //        bool sign = false;
        //        foreach (MoralityState state in stateInfo)
        //        {
        //            if (state.ClassID == info.RID && state.StartTime.Month == dt.Month)
        //            {
        //                // 构造超链html标签
        //                HyperLink control = new HyperLink();
        //                control.Text = "已录入";
        //                control.Attributes.Add("onclick", "detailView();");
        //                control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Type=Edit",
        //                                        year, term,
        //                                        state.ClassID, state.StartTime.Year, state.StartTime.Month);
        //                cell.Controls.Add(control);
        //                sign = true;
        //                break;
        //            }
        //        }
        //        if (!sign)
        //        {
        //            // 构造超链html标签
        //            HyperLink control = new HyperLink();
        //            control.Text = "未录入";
        //            control.Attributes.Add("onclick", "detailView();");
        //            control.NavigateUrl = String.Format("../ScoreInput/ScoreInput.aspx?SchoolYear={0}&Term={1}&ClsID={2}&Year={3}&Month={4}&Type=Edit",
        //                                    year, term,
        //                                    info.RID, dt.Year, dt.Month);
        //            cell.Controls.Add(control);
        //        }

        //        row.Cells.Add(cell);
        //    }

        //    tbResult.Rows.Add(row);
        //}
    }
}
